//
//  WJMainTabBar.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/19.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

class WJMainTabBar: UITabBar {

    var closure: (()->())?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

    private func setupUI() {
        
        addSubview(composeBtn)
        
        composeBtn.sizeToFit()
        
    }
    
    // layoutSubviews 设置子控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算UITabBarButton 宽度
        let w = frame.width * 0.2
        var index: CGFloat = 0
        //UITabBarButton
        // 遍历子控件
        for value in subviews {
            // 判断value 是否是UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                // 设置UITabBarButton frame
                value.frame.origin.x = index * w
                value.frame.size.width = w
                
                index++
                // 为了给撰写按钮留位置
                if index == 2 {
                    index++
                }
                
            }
        }
        // 设置composeButton
        composeBtn.center.x = frame.width * 0.5
        composeBtn.center.y = frame.height * 0.5
    }
    
    
    
    
    lazy var composeBtn: UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: "composeBtnClik", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        return btn
    }()
    
    func composeBtnClik() {
        
        closure?()
    }
    
}
